package com.baidu.sggc.controller;

import com.baidu.sggc.audit.extend.AuditExtendContext;
import com.baidu.sggc.audit.extend.GeneralMessage;
import com.baidu.sggc.core.annotation.AuditLog;
import com.baidu.sggc.core.common.R;
import com.baidu.sggc.data.req.CommentsReq;
import com.baidu.sggc.entity.Comments;
import com.baidu.sggc.service.CommentsService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 评论接口
 */
@RestController
@RequestMapping("/comments")
@Slf4j
public class CommentsController {

    @Autowired
    private CommentsService commentsService;

    /**
     * 添加评论
     * @return 统一响应 R<>
     */
    @PostMapping("/add")
    @AuditLog(module = "评论管理", auditObject = "用户评论", operation = "create", message = "新增用户评论")
    public R addComment(@RequestBody Comments comments) {
        commentsService.addComment(comments); // 此时 commentId 应已生成（可返回）
        // 假设 comments.getId() 在 service 层执行完后可获取
        GeneralMessage msg = new GeneralMessage();
        msg.setDataType("用户评论");
        msg.setDataName(""); // 或者用简短摘要
        // 方式一（推荐，简洁）：
        msg.setUniqueIdentification(Arrays.asList(String.valueOf(comments.getId())));
        msg.setOperationNum(1);
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> extendMap = mapper.convertValue(msg, Map.class);
        AuditExtendContext.setExtendData(extendMap);

        return R.ok();
    }

    /**
     * 获取所有评论
     * @return 统一响应 R<>
     */
    @PostMapping("/list")
    @AuditLog(module = "评论管理", auditObject = "用户评论", operation = "listQuery", message = "获取所有评论")
    public R<List<Comments>> getAllComments(@RequestBody CommentsReq commentsReq) {
        // 设置结构化扩展字段（QueryMessage）
        Map<String, Map<String, String>> extendData = new LinkedHashMap<>();
        Map<String, String> queryConditions = new HashMap<>();
        if (commentsReq.getUserId() != null) {
            queryConditions.put("用户ID", commentsReq.getUserId());
        }
        if (commentsReq.getTargetId() != null) {
            queryConditions.put("目标ID", commentsReq.getTargetId());
        }
        if (commentsReq.getBizType() != null) {
            queryConditions.put("类型", commentsReq.getBizType());
        }
        extendData.put("查询条件", queryConditions);
        Map<String, String> sortInfo = new HashMap<>();
        sortInfo.put("创建时间", "降序");
        extendData.put("排序", sortInfo);
        Map<String, String> timeRange = new HashMap<>();
        timeRange.put("开始时间", Optional.ofNullable(commentsReq.getStartTime()).map(Object::toString).orElse(""));
        timeRange.put("结束时间", Optional.ofNullable(commentsReq.getEndTime()).map(Object::toString).orElse(""));
        extendData.put("时间范围", timeRange);
        AuditExtendContext.setExtendData((Map) extendData);
        List<Comments> allComments = commentsService.getAllComments(commentsReq);
        return R.ok(allComments);
    }
}
